<template>
  <el-card style="margin-bottom:20px;">
    <el-form :inline="true" :model="formData" class="demo-form-inline">
      <el-form-item label="一级分类">
        <!-- 当进入添加|修改界面时 则三级分类被禁用 -->
        <el-select :disabled="!isShowCont" v-model="formData.category1Id" placeholder="分类名称" @change="handlerC1">
          <el-option :label="c1.name" :value="c1.id" v-for="c1 in category1List" :key="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select :disabled="!isShowCont" v-model="formData.category2Id" placeholder="分类名称" @change="handlerC2">
          <el-option :label="c2.name" :value="c2.id" v-for="c2 in category2List" :key="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select :disabled="!isShowCont" v-model="formData.category3Id" placeholder="分类名称" @change="handlerC3">
          <el-option :label="c3.name" :value="c3.id" v-for="c3 in category3List" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name:'CategoryList',
  props:['isShowCont'],
  data() {
    return {
      val: "",
      // 一级分类数据
      category1List: [],
      // 二级分类数据
      category2List: [],
      // 三级分类数据
      category3List: [],
      // 各分类Id
      formData: {
        category1Id: "",
        category2Id: "",
        category3Id: ""
      }
    };
  },
  mounted() {
    this.getCategoryList();
  },
  methods: {
    // 获取一级联动数据
    async getCategoryList() {
      let result = await this.$api.attr.reqCategory1List();
      if (result.code === 200) {
        this.category1List = result.data;
      }
    },
    // 一级分类变化事件
    async handlerC1() {
      // 清空后两级的内容
      this.category2List = []
      this.category3List = []
      this.formData.category2Id = ''
      this.formData.category3Id = ''

      // 将 一级id 传给父组件
      let {category1Id} = this.formData
      this.$emit('categoryInfo', {categoryId:category1Id,level:1});
      // 获取 二级分类数据
      let result = await this.$api.attr.reqCategory2List(category1Id);
      if (result.code === 200) {
        this.category2List = result.data;
      }
    },
    // 二级分类变化事件
    async handlerC2() {
      // 清空三级分类
      this.category3List = []
      this.formData.category3Id = ''
      
      // 将 二级id 传给父组件
      let {category2Id} = this.formData
      this.$emit('categoryInfo', {categoryId:category2Id,level:2});
      // 获取 三级级分类数据
      let result = await this.$api.attr.reqCategory3List(category2Id);
      if (result.code === 200) {
        this.category3List = result.data;
      }
    },
    // 三级分类变化事件
    async handlerC3() {
      // let result = await this.$api.attr.reqFindCategoryList(category1Id,category2Id,category3Id);
      // if (result.code === 200) {
      //   this.$emit('categoryInfo', result.data,category3Id);
      // }
      // 将 三级id 传给父组件
      let {category3Id} = this.formData
      this.$emit('categoryInfo', {categoryId:category3Id,level:3});
    },
  }
}
</script>

<style>

</style>